import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'dart:convert';

import 'package:flutter_shop/model/cartInfo.dart';
import 'package:flutter_shop/pages/cart_page/cart_count.dart';
import 'package:flutter_shop/provide/cart.dart';
import 'package:provide/provide.dart';

class CartItem extends StatelessWidget {
  CartInfoMode cartInfoMode;

  CartItem({Key key, this.cartInfoMode}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Container(
      margin: EdgeInsets.fromLTRB(5.0, 2.0, 5.0, 2.0),
      padding: EdgeInsets.fromLTRB(5.0, 10.0, 5.0, 10.0),
      decoration: BoxDecoration(
        color: Colors.white,
          border:
              Border(bottom: BorderSide(color: Colors.black12, width: 0.5))),
      child: Row(
        children: <Widget>[
          _cartCheckBt(context),
          _cartImage(),
          _cartGoodName(),
          _cartPrice(context)
        ],
      ),
    );
  }

  //多选按钮
  Widget _cartCheckBt(BuildContext context) {
    print('------------------------------${cartInfoMode.isCheck}');
    return Container(
      alignment: Alignment.center,
      child: Checkbox(
        value: cartInfoMode.isCheck,
        onChanged: (bool val) {

          Provide.value<CartProvide>(context).changeCheckState(cartInfoMode);
        },
        activeColor: Colors.pink,
      ),
    );
  }

  //图片
  Widget _cartImage() {
    return Container(
      width: ScreenUtil().setWidth(150.0),
      height: ScreenUtil().setHeight(150.0),
      padding: EdgeInsets.all(3.0),
      decoration:
          BoxDecoration(border:Border.all(width: 0.5,color: Colors.pink)),
      child: Image.network(cartInfoMode.images),
    );
  }

  //商品名称

  Widget _cartGoodName() {
    return Container(
      width: ScreenUtil().setWidth(300.0),
      padding: EdgeInsets.all(10.0),
      alignment: Alignment.centerLeft,
      child:Column(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: <Widget>[
          Text(
            cartInfoMode.goodsName,
            style: TextStyle(fontSize: ScreenUtil().setSp(25.0)),
          ),
          SizedBox(height: ScreenUtil().setHeight(30.0),),
          CartCount(cartInfoMode: cartInfoMode,),
        ],
      ),
    );
  }

  //商品价格
  Widget _cartPrice(BuildContext context) {
    return Container(
      alignment: Alignment.topRight,
      width: ScreenUtil().setWidth(150.0),
      child: Column(
        children: <Widget>[
          Text(
            '￥${cartInfoMode.price}',
            style: TextStyle(
                color: Colors.pink, fontSize: ScreenUtil().setSp(30.0)),
          ),
          SizedBox(height: ScreenUtil().setHeight(30.0),),
          Container(
            child: InkWell(
              onTap: ()async{
                await Provide.value<CartProvide>(context).deleteOneGoods(cartInfoMode.goodsId);
              },
              child: Icon(Icons.delete,color: Colors.black26,size: 30.0,),
            ),
          )
        ],
      ),
    );
  }
}
